// pages/cate/subcategory/subcategory.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pagenum: 0,
    scroll: 0,
    pagesize: 10,
    isLoad: false,
    goods: <any>[],
    query: "",
    Temporary: "",
    isEnd: false,
    moreText: "该分类暂时没有哦",
  },
  async getGoods() {
    let { pagenum, pagesize, query, isLoad, goods, isEnd, Temporary } =
      this.data;
      if (isEnd) return;
      this.setData({ isLoad: true });
    if (isLoad) return;
    ++pagenum;
    // 如果query为空说明，说明用户重新进行搜索
    const empty = query === "";
    if (!query) {
      query = Temporary;
    } else {
      Temporary = query;
    }
    const params: object = { query, pagenum, pagesize };
    const { message } = await wx.http.get("/goods/search", params);
    if (empty) {
      goods = [...goods, ...message.goods];
    } else {
      goods = message.goods;
    }
    const moreText = goods.length > 0 ? "没有更多了" : "该分类暂时没有哦";
    if (message.total === goods.length) {
     isEnd=true
    }
    this.setData({
      goods,
      query: "",
      isEnd,
      pagenum,
      Temporary,
      isLoad: false,
      moreText,
    });
  },
  // 键盘输入事件
  search() {
    let { query, isEnd } = this.data;
    console.log(query, isEnd);

    if (!query.trim()) {
      wx.showToast({ title: "请输入商品名", icon: "error" });
      return;
    }
    isEnd = false;
    this.setData({ isEnd, scroll: 0 });
    this.getGoods();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: { query: string }) {
    const { query } = options;
    this.setData({
      query,
    });
    this.getGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
